//
//  QLBuyGiftView.m
//  神约
//
//  Created by qlwl on 17/2/7.
//  Copyright © 2017年 qlwl. All rights reserved.
//

#import "QLBuyGiftView.h"

@interface QLBuyGiftView ()
@property (weak, nonatomic) IBOutlet UIImageView *giftImaView;
@property (weak, nonatomic) IBOutlet UITextField *giftNumField;
@property (nonatomic, copy) BuyGiftNum buyGiftClick ; //
@end

@implementation QLBuyGiftView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
+(instancetype)getMyGiftView
{
    return [[[NSBundle mainBundle] loadNibNamed:@"QLBuyGiftView" owner:self options:nil] lastObject];
}
- (IBAction)buyGiftBtnClick:(id)sender {
    if (self.buyGiftClick) {
        self.buyGiftClick([self.giftNumField.text intValue]);
    }
    [self remove];
}

- (void)showInView:(UIView *)superView andBuyClick:(BuyGiftNum)buyClick
{
    [superView addSubview:self];
    self.buyGiftClick = [buyClick copy];
    self.alpha = 0;
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 1;
    }];
}
- (void)remove
{
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.frame = self.superview.bounds;
}
+(instancetype)showInWindowBuyNum:(BuyGiftNum)buyClick
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    QLBuyGiftView * giftView = [self getMyGiftView];
    [giftView showInView:window andBuyClick:[buyClick copy]];
    return giftView;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if ([touches isEqualToSet:[event touchesForView:self]]) {
        [self remove];
    }
}


@end
